package com.ktg.mes.unit.sync.sql;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.mes.unit.sync.SQLModel;
import com.ktg.mes.unit.sync.SyncModel;
import com.ktg.mes.unit.sync.sql.mapper.SyncMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class SyncServiceImpl extends ServiceImpl<SyncMapper, SyncModel> implements SyncService {

    // spring注入ApplicationContext
    @Autowired
    private ApplicationContext context;

    /***
     *动态初始化Service
     * @return
     */
    @SuppressWarnings("unchecked")
    public  IService getService(String className) {
        try {
            Class clazz = Class.forName(className);
            // 逻辑处理你需要实例化的类
            String beanName =  clazz.getSimpleName();
            String firstLowerBeanName = beanName.substring(0,1).toLowerCase()+beanName.substring(1,beanName.length());
            Object obj = context.getBean(firstLowerBeanName, clazz);
            if (obj instanceof IService ){
                if (obj instanceof ServiceImpl){
                    return  (IService)obj;
                }else {
                    throw new RuntimeException(" className 需要继承 com.baomidou.mybatisplus.core.mapper.BaseMapper");
                }
            }else {
                throw new RuntimeException(" className 需要继承 com.baomidou.mybatisplus.extension.service.IService");
            }
        } catch (Exception e) {
            System.out.println("请传入正确的 className");
            log.error("同步：updateSql",e);
        }
        return null;
    }

    @Override
    public List<Map> selectSqlMap(String selectSglString) {
        try {
            return getBaseMapper().selectSqlMap(selectSglString);
        }catch (Exception e){
            log.error("同步：updateSql",e);
        }
        return null;
    }

    @Override
    public void updateSql(String updateSglString) {
        try {
            getBaseMapper().updateSql(updateSglString);
        }catch (Exception e){
            log.error("同步：updateSql",e);
        }
    }

    @Override
    public void insertSql(String insertSglString) {
        try {
            getBaseMapper().insertSql(insertSglString);
        }catch (Exception e){
            log.error("同步：updateSql",e);
        }
    }

    @Override
    public void insertModel(SQLModel sqlModel){
        try {
            getBaseMapper().insertModel(sqlModel);
        }catch (Exception e){
            log.error("同步：updateSql",e);
        }
    };

    @Override
    public void updateModel(SQLModel sqlModel){
        try {
            getBaseMapper().updateModel(sqlModel);
        }catch (Exception e){
            log.error("同步：updateSql",e);
        }
    };

    public List<Map> selectSqlModel(SQLModel sqlModel){
        try {
           return getBaseMapper().selectSqlModel(sqlModel);
        }catch (Exception e){
            log.error("同步：updateSql",e);
        }
        return null;
    };

//    private int size;
//    public void synsStart(SyncModel model){
//        size = model.getSize();
//        int count = 0;
//        int page = 0;
//        Integer updateValue = model.get
//        do {
//            String findSql = getSelectSqlString(page,model);
//            List<Map> list = getBaseMapper().selectMap(findSql);
//
//        }while (count < size);
//
//
//    }
//
//    public String getSelectSqlString(int page,SyncModel model){
//        String columnStr = "";
//        for (int i = 0; i < model.getToColumnList().size(); i++) {
//            String colu = model.getFromColumnList().get(i)+" as "+model.getToColumnList().get(i);
//            if (i == 0){
//                columnStr = colu;
//            }else {
//                columnStr += "," + colu;
//            }
//        }
//        String str = "selcet "+columnStr+" from "+ model.getFromTable() +" where "+model.getSyncKey()+" > "+model.getMoreThan()+" order by "+model.getSyncKey()+" asc limit " +(page*size)+" "+size;
//        return str;
//    }
//
//    public String getSelectSqlString(int page,SyncModel model){
//        String columnStr = "";
//        for (int i = 0; i < model.getToColumnList().size(); i++) {
//            String colu = model.getFromColumnList().get(i)+" as "+model.getToColumnList().get(i);
//            if (i == 0){
//                columnStr = colu;
//            }else {
//                columnStr += "," + colu;
//            }
//        }
//        String str = "selcet "+columnStr+" from "+ model.getFromTable() +" where "+model.getSyncKey()+" > "+model.getMoreThan()+" order by "+model.getSyncKey()+" asc limit " +(page*size)+" "+size;
//        return str;
//    }



    public boolean save(Object object,String iServiceImpl_Name){
        if (object == null){
            return false;
        }
        IService one = (IService) getService(iServiceImpl_Name);
        if (one != null){
            return one.save(object);
        }
        return false;
    }


}
